package org.javaboy.vhr.service;

import org.javaboy.vhr.mapper.MenuMapper;
import org.javaboy.vhr.mapper.MenuRoleMapper;
import org.javaboy.vhr.model.Hr;
import org.javaboy.vhr.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;

    /**
     * 根据hr对象id获取所有的菜单列表
     * @return
     */
    public List<Menu> getMenuList() {
        return menuMapper.getMenuListById(((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }

    /**
     * 查询菜单所需要的角色
     * 缓存Cacheable
     * @return
     */
    // @Cacheable
    public List<Menu> getAllMenuWithRole(){
        return menuMapper.getAllMenuWithRole();
    }

    public List<Menu> getAllMenu() {
        return menuMapper.getAllMenu();
    }

    public List<Integer> getMenuIdByRid(Integer rid) {
        return menuRoleMapper.getMenuIdByRid(rid);
    }

    @Transactional
    public Boolean updatePowerByRid(Integer rid, Integer[] mIds) {
        // 根据rid删除之前的数据
        menuRoleMapper.deleteByRid(rid);
        // 再重新新增新的数据
        if(mIds!=null && mIds.length>0){
            return menuRoleMapper.addByRidAndMids(rid,mIds)==mIds.length;
        }
        return true;
    }
}
